package com.gitee.wsl.compose.chart.xy.bar

import androidx.compose.runtime.Composable
import com.gitee.wsl.compose.chart.xy.bar.model.BarEntry


/**
 * Scope item to allow adding items to a [VerticalBarPlot].
 */
 interface VerticalBarPlotScope<X, Y> {
    /**
     * Adds an item at the specified [x] axis coordinate, with vertical extent spanning from
     * [yMin] to [yMax]. An optional [bar] can be provided to customize the Composable used to
     * generate the bar for this specific item.
     */
     fun item(x: X, yMin: Y, yMax: Y, bar: (@Composable BarScope.() -> Unit)? = null)
}

internal class VerticalBarPlotScopeImpl<X, Y>(
    private val defaultBar: @Composable BarScope.() -> Unit
) : VerticalBarPlotScope<X, Y> {

    val data: MutableMap<X, Pair<BarEntry<X, Y>, @Composable BarScope.() -> Unit>> =
        mutableMapOf()

    override fun item(x: X, yMin: Y, yMax: Y, bar: (@Composable BarScope.() -> Unit)?) {
        data[x] = Pair(BarEntry(x, yMin, yMax), bar ?: defaultBar)
    }
}